package com.cskaoyan.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.mall.product.cache.RedisCache;
import com.cskaoyan.mall.product.converter.dto.SpuInfoConverter;
import com.cskaoyan.mall.product.converter.param.SpuInfoParamConverter;
import com.cskaoyan.mall.product.dto.SpuImageDTO;
import com.cskaoyan.mall.product.dto.SpuInfoPageDTO;
import com.cskaoyan.mall.product.dto.SpuPosterDTO;
import com.cskaoyan.mall.product.dto.SpuSaleAttributeInfoDTO;
import com.cskaoyan.mall.product.mapper.*;
import com.cskaoyan.mall.product.model.*;
import com.cskaoyan.mall.product.query.SpuInfoParam;
import com.cskaoyan.mall.product.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author Pudding
 * @Date 2024/7/1 17:48
 */
@Service
public class SpuServiceImpl implements SpuService {

    @Autowired
    SpuInfoMapper spuInfoMapper;
    @Autowired
    SpuInfoConverter spuInfoConverter;
    @Autowired
    SpuInfoParamConverter spuInfoParamConverter;
    @Autowired
    SpuImageMapper spuImageMapper;
    @Autowired
    SpuPosterMapper spuPosterMapper;
    @Autowired
    SpuSaleAttrInfoMapper spuSaleAttrInfoMapper;
    @Autowired
    SpuSaleAttrValueMapper spuSaleAttrValueMapper;
    @Autowired
    SkuSaleAttrValueMapper skuSaleAttrValueMapper;

    //1. 根据分页参数查询SPU分页数据
    @Override
    public SpuInfoPageDTO getSpuInfoPage(Page<SpuInfo> pageParam, Long category3Id) {

        //1.1 构建查询条件
        LambdaQueryWrapper<SpuInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SpuInfo::getThirdLevelCategoryId,category3Id);
        //1.2 进行分页查询，spuInfoPage包含了总条数和数据
        Page<SpuInfo> spuInfoPage = spuInfoMapper.selectPage(pageParam, lambdaQueryWrapper);
        //1.3 获取当前页的记录
        List<SpuInfo> records = spuInfoPage.getRecords();
        Integer total = (int) spuInfoPage.getTotal();
        //1.4 将上述数据封装到SpuInfoPageDTO中
        SpuInfoPageDTO spuInfoPageDTO = new SpuInfoPageDTO(spuInfoConverter.spuInfoPOs2DTOs(records), total);

        return spuInfoPageDTO;
    }

    //2. 保存完整的SPU信息
    @Override
    public void saveSpuInfo(SpuInfoParam spuInfoParam) {
        //2.1 现将参数对象转化为PO对象
        SpuInfo spuInfo = spuInfoParamConverter.spuInfoParam2Info(spuInfoParam);
        //2.2 查询基本spuInfo信息到spu_info表
        spuInfoMapper.insert(spuInfo);
        //2.3 获取spuImage集合信息，保存图片到spu_image表
        for (SpuImage spuImage : spuInfo.getSpuImageList()) {
            //给spuImage的spuId属性赋值，因为只有将spu基本信息插入数据库spuInfo的id才有值
            spuImage.setSpuId(spuInfo.getId());
            spuImageMapper.insert(spuImage);
        }
        //2.4 获取到posterList集合数据。保存多条海报信息到spu_poster表
        for (SpuPoster spuPoster : spuInfo.getSpuPosterList()) {
            //给spuPoster的spuId赋值
            spuPoster.setSpuId(spuInfo.getId());
            spuPosterMapper.insert(spuPoster);
        }
        //2.5 获取到spuSaleAttrInfoList集合数据。保存多条销售属性信息到spu_sale_attribute_info表
        for (SpuSaleAttributeInfo spuSaleAttributeInfo : spuInfo.getSpuSaleAttributeInfoList()) {
            //2.5.1 将spuSaleAttributeInfo信息插入到spu_sale_attribute_info表
            spuSaleAttributeInfo.setSpuId(spuInfo.getId());
            spuSaleAttrInfoMapper.insert(spuSaleAttributeInfo);

            //2.5.2 遍历销售属性值集合spuSaleAttributeValueList，保存多条数据到spu_sale_attr_value表
            for (SpuSaleAttributeValue spuSaleAttributeValue : spuSaleAttributeInfo.getSpuSaleAttrValueList()) {
                //2.5.3 将spuSaleAttributeValue信息插入到spu_sale_attr_value表
                spuSaleAttributeValue.setSpuId(spuInfo.getId());
                spuSaleAttributeValue.setSpuSaleAttrId(spuSaleAttributeInfo.getId());
                spuSaleAttrValueMapper.insert(spuSaleAttributeValue);
            }
        }
    }

    //3. 根据spuId获取指定spu中包含的图片集合
    @Override
    public List<SpuImageDTO> getSpuImageList(Long spuId) {

        //3.1 构造查询条件
        LambdaQueryWrapper<SpuImage> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SpuImage::getSpuId,spuId);
        //3.2 进行查询
        List<SpuImage> spuImageList = spuImageMapper.selectList(lambdaQueryWrapper);
        //3.3 将PO对象的list转换为DTO对象的list
        List<SpuImageDTO> spuImageDTOS = spuInfoConverter.spuImagePOs2DTOs(spuImageList);
        return spuImageDTOS;
    }

    //4. 根据SpuId查询SPU与销售属性关系表中包含的销售属性以及销售属性值集合
    @Override
    public List<SpuSaleAttributeInfoDTO> getSpuSaleAttrList(Long spuId) {

        //4.1 查询对应的SPU与销售属性关系表 List数据
        LambdaQueryWrapper<SpuSaleAttributeInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpuSaleAttributeInfo::getSpuId,spuId);
        List<SpuSaleAttributeInfo> spuSaleAttributeInfos = spuSaleAttrInfoMapper.selectList(queryWrapper);

        //4.2 遍历List<SpuSaleAttributeInfo>为了根据spu_sale_attr_info的id查询销售属性值表List信息（一对多关系）
        for (SpuSaleAttributeInfo spuSaleAttributeInfo : spuSaleAttributeInfos) {
            //构造查询条件
            LambdaQueryWrapper<SpuSaleAttributeValue> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(SpuSaleAttributeValue::getSpuSaleAttrId, spuSaleAttributeInfo.getId());
            List<SpuSaleAttributeValue> spuSaleAttributeValueList = spuSaleAttrValueMapper.selectList(lambdaQueryWrapper);

            //4.3 将查询的spuSaleAttributeValueList放到spuSaleAttributeInfo中的属性中
            spuSaleAttributeInfo.setSpuSaleAttrValueList(spuSaleAttributeValueList);
        }

        //4.4 最后将销售属性关系表List对应的PO对象转换为DTO对象
        List<SpuSaleAttributeInfoDTO> spuSaleAttributeInfoDTOS = spuInfoConverter.spuSaleAttributeInfoPOs2DTOs(spuSaleAttributeInfos);
        return spuSaleAttributeInfoDTOS;
    }

    //5. 根据spuId获取sku商品的海报列表
    @Override
    @RedisCache(prefix = "spuPosterList:")
    public List<SpuPosterDTO> findSpuPosterBySpuId(Long spuId) {

        //5.1 构造查询条件
        LambdaQueryWrapper<SpuPoster> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SpuPoster::getSpuId, spuId);
        //5.2 进行查询
        List<SpuPoster> spuPosters = spuPosterMapper.selectList(lambdaQueryWrapper);
        //5.3 将PO对象转换后为DTO对象
        List<SpuPosterDTO> spuPosterDTOS = spuInfoConverter.spuPosterPOs2DTOs(spuPosters);

        return spuPosterDTOS;
    }

    //6. 根据spuId查询销售属性值的组合（这个是sku_sale_attr_value表） 和 skuId的映射关系
    @Override
    @RedisCache(prefix = "skuValueIdsMap:")
    public Map<String, Long> getSkuValueIdsMap(Long spuId) {

        //6.1 自定义xml进行查询
        List<SkuSaleAttributeValuePermutation> skuSaleAttributeValuePermutations = skuSaleAttrValueMapper.selectSaleAttrValuesBySpu(spuId);
        // 销售属性值的组合				  skuId
        // 冰岛幻境，8+128G    ----------- 15
        // 冰岛幻境，8+256G    ----------- 16
        // 幻夜星河，8+128G    ----------- 13
        // 幻夜星河，8+256G    ----------- 14
        // 3770|3773    ----------- 13
        // 3770|3774    ----------- 14
        Map<String, Long> map = skuSaleAttributeValuePermutations.stream()
                .collect(Collectors.toMap(SkuSaleAttributeValuePermutation::getSkuSaleAttrValuePermutation, SkuSaleAttributeValuePermutation::getSkuId));

        return map;
    }
}
